<?php
class Acls extends MY_Controller 
{
	var $curl = 'acls';
	var $name = '';
	var $search = '';
	var $per_page = 0;
	var $language;
	var $submenu = "_menu";
	
	function __construct()
	{
		parent::__construct();
		
		$this->load->model(array('/public/acl_role_model', '/public/account_resource_model', '/public/account_permission_model'));

		$this->validation->set_fields($this->acl_role_model->fields);
		
		$this->data['Title'] = T_('Распределение доступа');
		
		$this->name = T_('Распределение доступа');
		
		$this->language = parent::getLang();		
	}
		
	function roles()
	{
		$this->acl_role_model->search();

		$this->data['Title'] .= ' / '. T_('Роли пользователей'); 
		
		$this->submenu = "_menu_roles";
		
		$this->renderLayout();
	}

	function create_role()
	{
		$this->validation->set_fields($this->acl_role_model->fields);
		
		if (count($_POST))
		{
			$rules = array('name', 'order', 'description');
			$this->validation->set_rules($this->acl_role_model->getRules($rules));
						
			if ($this->validation->run() === TRUE)
			{
				$item['name'] 	= $this->validation->name;
				$item['description'] = $this->validation->description;				
				$item['order']	= $this->validation->order;
				
				$this->acl_role_model->insert($item);

				redirect($this->curl .'/roles');
			}
		}
		
		$this->submenu = "_menu_roles";
		
		$this->renderLayout();			
	}

	function edit_role($id = null)
	{
		$this->validation->set_fields($this->acl_role_model->fields);
		
		if (!empty($id))
		{
			$object = $this->acl_role_model->get($id);

			if (!count($object))
			{
				$this->renderElement('query_no_object');
				return false; 
			}
			
			if (!count($_POST))
			{
				$this->validation->id 	 = $object['id'];
				$this->validation->name  = $object['name'];
				$this->validation->description  = $object['description']; 
				$this->validation->order = $object['order'];			 
			}
			else
			{
				$rules = array('name', 'order', 'description');
				$this->validation->set_rules($this->acl_role_model->getRules($rules));
	
				if ($this->validation->run() === TRUE)
				{
					$item['id'] 	= $this->validation->id;
					$item['name'] 	= $this->validation->name;
					$item['description'] 	= $this->validation->description;
					$item['order'] 	= $this->validation->order;
					
					$this->acl_role_model->update($item);
					
					redirect($this->curl .'/roles');
				}
			}
		}
	
		$this->submenu = "_menu_roles";
		
		$this->renderLayout();
	}
		
	function deleteItems()
	{
		$data = '';
		$type = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : '';
		
		switch($type)
		{
			case 'roles':
				$data = $this->acl_role_model->delete($_REQUEST['items']);		
			break;
			
			case 'resources':
				$data = $this->account_resource_model->delete($_REQUEST['items']);		
			break;
		}
		
		echo $data;
	}

	function delete_role($id)
	{
		$data = $this->acl_role_model->delete($id);

		redirect($this->curl .'/roles');
	}
	
	function resources()
	{
		$this->account_resource_model->search();

		$this->data['Title'] .= ' / '. T_('Ресурсы сайта'); 
		
		$this->submenu = "_menu_resources";
				
		$this->renderLayout();
	}

	function create_resource()
	{
		$this->validation->set_fields($this->account_resource_model->fields);
		
		if (count($_POST))
		{
			$rules = array('category', 'controller', 'action', 'description');
			$this->validation->set_rules($this->account_resource_model->getRules($rules));
						
			if ($this->validation->run() === TRUE)
			{
				$item['category']	 = $this->validation->category;
				$item['controller']	 = $this->validation->controller;
				$item['action']		 = $this->validation->action;
				$item['description'] = $this->validation->description;
				
				$this->account_resource_model->insert($item);

				redirect($this->curl .'/resources');
			}
		}

		$this->submenu = "_menu_resources";
		
		$this->renderLayout();			
	}
		
	function edit_resource($id = null)
	{
		$this->validation->set_fields($this->account_resource_model->fields);
		
		if (!empty($id))
		{
			$object = $this->account_resource_model->get($id);
			
			if (!count($object))
			{
				$this->renderElement('query_no_object');
				return false; 
			}
			
			if (!count($_POST))
			{
				$this->validation->id 	 		= $object['id'];
				$this->validation->category 	= $object['category']; 
				$this->validation->controller 	= $object['controller'];
				$this->validation->action  		= $object['action']; 
				$this->validation->description  = $object['description']; 			 
			}
			else
			{
				$rules = array('category', 'controller', 'action', 'description');
				$this->validation->set_rules($this->account_resource_model->getRules($rules));
	
				if ($this->validation->run() === TRUE)
				{
					$item['id'] 		 = $this->validation->id;
					$item['category']	 = $this->validation->category;
					$item['controller']	 = $this->validation->controller;
					$item['action']		 = $this->validation->action;
					$item['description'] = $this->validation->description;					
					
					$this->account_resource_model->update($item);
					
					redirect($this->curl .'/resources');
				}
			}
		}
	
		$this->submenu = "_menu_resources";
		
		$this->renderLayout();
	}

	function delete_resources()
	{
		$data = $this->account_resource_model->delete($_REQUEST['items']);

		echo $this->utils->php2js($data);
	}	
	
	function permissions($id_role = null)
	{
		if (count($_POST))
		{
			if (!empty($_POST['rid']))
			{
				$sql = 'TRUNCATE TABLE "public"."account_permissions";';
				$this->db->query($sql);
				
				foreach($_POST['rid'] as $rid)
				{
					$x = explode('_', $rid);
					$item['id_account_resources'] 	= $x[0];
					$item['id_account_type'] 		= $x[1];
					$this->account_permission_model->insert($item);		
				}
				
				$this->log->writeMessage('done', T_('Права доступа сохранены.'));
	
				$log['id_admins'] = $this->auth->getID();
				$log['message']   = T_('Права доступа сохранены.');
				$log['location']  = $_SERVER['REQUEST_URI'];
				$this->watchdog_model->insert($log);	

				redirect($this->curl . '/permissions/'. $id_role);
			}			
		}
		
		$this->account_permission_model->search();
		
		$this->data['roles'] = $this->acl_role_model->getSelectOptions($id_role);		
		$this->data['resources'] = $this->account_resource_model->getAll();
		$this->data['permissions'] = $this->account_permission_model->get();
		$this->data['id_role'] = $id_role;
		
		$this->data['Title'] .= ' / '. T_('Доступы'); 
		
		$this->submenu = "_menu_permissions";
				
		$this->renderLayout();
	}
}
?>